package com.wedo.mall.activity;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.bumptech.glide.Glide;
import com.e.library.activity.EActivity;
import com.e.library.utils.ESPUtils;
import com.wedo.mall.R;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.BindViews;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.OnPageChange;

/**
 *
 * 引导页
 * Created by lxy on 2017/1/7.
 */

public class GuideActivity extends EActivity {

    @BindView(R.id.ll_btn) LinearLayout ll_btn;
    @BindView(R.id.vp_guide) ViewPager vp_guide;
    @BindViews({R.id.v_indicate1,R.id.v_indicate2,R.id.v_indicate3}) List<View> indicates;

    @Override
    protected int layout() {
        return R.layout.activity_gudie;
    }

    @Override
    protected void onActivityCreated(Bundle bundle) {
        disableSwipeBack();

        List<View> pages = new ArrayList<>(3);
        GuidePage page1 = new GuidePage(this,R.drawable.ic_guide1);
        GuidePage page2 = new GuidePage(this,R.drawable.ic_guide2);
        GuidePage page3 = new GuidePage(this,R.drawable.ic_guide3);

        pages.add(page1.getView());
        pages.add(page2.getView());
        pages.add(page3.getView());

        GuideAdapter adapter = new GuideAdapter(pages);
        vp_guide.setAdapter(adapter);
        onPageChanged(0);
    }

    @OnClick(R.id.btn_tourist)
    protected void onTouristClick(){
        toTarget(MainActivity.class);
    }

    @OnClick(R.id.btn_login)
    protected void onLoginClick(){
        toTarget(LoginActivity.class);
    }

    private void toTarget(Class<?> clazz){
        ESPUtils.setboolean(this,"to_guide",false);
        toActivity(clazz);
        defaultFinish();
    }
    @OnPageChange(R.id.vp_guide)
    protected void onPageChanged(int position){
        for (View view:indicates){
            view.setSelected(false);
        }

        indicates.get(position).setSelected(true);
        ll_btn.setVisibility(position==2?View.VISIBLE:View.GONE);
    }

    public static class GuidePage{
        private View layout;
        @BindView(R.id.iv_guide) ImageView iv_guide;

        public GuidePage(Context context,int guide) {
            layout = LayoutInflater.from(context).inflate(R.layout.page_guide,null);
            ButterKnife.bind(this,layout);
            Glide.with(context).fromResource().load(guide).into(iv_guide);
        }

        public View getView() {
            return layout;
        }
    }

    private static class GuideAdapter extends PagerAdapter{

        private List<View> mPages;

        public GuideAdapter(List<View> mPages) {
            this.mPages = mPages;
        }

        @Override
        public int getCount() {
            return mPages.size();
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            View page = mPages.get(position);
            container.addView(page);
            return page;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView(mPages.get(position));
        }
    }

}
